package com.abigdreamer.im.client.module.login;

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JLabel;

import com.abigdreamer.im.client.module.network.socket.ClientChatSocket;
import com.abigdreamer.im.client.resourse.ResourcesManagement;
import com.abigdreamer.im.client.ui.BaseFrame;
import com.abigdreamer.im.client.ui.trayIcon.EIMTrayIcon;
import com.abigdreamer.im.client.util.EIMClientConfig;
import com.abigdreamer.swing.EmptyComponent;
import com.abigdreamer.swing.component.JCButton;
import com.abigdreamer.swing.component.JCLabel;
import com.sun.naming.internal.ResourceManager;

import craky.componentc.JCFrame;
import craky.layout.LineLayout;

public class WaitLoginFrame extends JCFrame {

	private static final long serialVersionUID = 2685950107772586906L;
	
	private static WaitLoginFrame instance;

	/**
	 * @return
	 */
	public static WaitLoginFrame getInstance() {
		if (instance == null) {
			instance = new WaitLoginFrame();
		}
		return instance;
	}

	/** Creates new form QQWaitLoginFrame */
	private WaitLoginFrame() {
		setSize(290, 660);
		setTitle("ABD-IM 2012");
		initComponents();
//		this.setResizable(false);
	}

	public void changTryIcon() {
		EIMTrayIcon trayIcon = EIMTrayIcon.getInStance();
		trayIcon.setFrame(this);
		trayIcon.setTrayIconState(EIMClientConfig.LoginingTryIcon_Type);
		trayIcon.showIcon(EIMClientConfig.LoginingTryIcon_Type);
	}

	private void initComponents() {
		this.setAlwaysOnTop(true);
//		setBackgroundImage(ResourcesManagement.getImage("ui/LoginPanel_window_windowBkg.png"));
		quxiao_Button = new JCButton("\u53d6\u6d88\u767b\u5f55");
		jLabel1 = new JCLabel("\u6b63\u5728\u767b\u5f55........");

		JLabel loading = new JLabel();
		loading.setIcon(ResourcesManagement.getImageIcon("ui/Logining.gif"));

		quxiao_Button.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				quxiao_ButtonMouseClicked(evt);
			}
		});


		EmptyComponent ecHeadLabel = new EmptyComponent();
		ecHeadLabel.setPreferredSize(new Dimension(290, 300));
		ecHeadLabel.setLayout(new LineLayout(25, 30, 0, 70, 0, LineLayout.CENTER, LineLayout.LEADING, LineLayout.VERTICAL));
		ecHeadLabel.add(loading, LineLayout.START);
		ecHeadLabel.add(jLabel1, LineLayout.START);
		ecHeadLabel.add(quxiao_Button, LineLayout.START);

		getContentPane().add(ecHeadLabel);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				setVisible(false);
				closedChatSocket();
				EIMLoginWindow loginFrame = EIMLoginWindow.getInstance();
				loginFrame.changTryIcon();
				BaseFrame.centerWindow(loginFrame);
				loginFrame.setVisible(true);
			}
		});

		addWindowListener(new WindowAdapter() {
			public void windowIconified(WindowEvent e) {

			}
		});

	}

	/**
	 * 取消按钮事件
	 * 
	 * @param evt
	 */
	private void quxiao_ButtonMouseClicked(java.awt.event.MouseEvent evt) {
		this.setVisible(false);
		EIMLoginWindow loginFrame = EIMLoginWindow.getInstance();
		BaseFrame.centerWindow(loginFrame);
		loginFrame.changTryIcon();
		loginFrame.setVisible(true);
		closedChatSocket();
	}

	public void closedChatSocket() {
		ClientChatSocket chatSocket = ClientChatSocket.getInstance();
		chatSocket.closedSocket();
	}

	private javax.swing.JLabel jLabel1;
	private javax.swing.JButton quxiao_Button;

}
